﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Dali.Command;
using Dali.Response;
using System.Diagnostics;
using System.Threading;
using Windows.Devices.Usb;
using Windows.Devices.Enumeration;
using System.Threading.Tasks;

namespace DaliUsb
{
  public class TridonicDaliUsb
  {
    private UsbDevice device;


    private async Task<UsbDevice> OpenDevice()
    {
      UInt32 vid = 0x17b5;
      UInt32 pid = 0x0020;
      DeviceInformationCollection devices;
      string aqs;

      aqs = "System.Devices.InterfaceEnabled:=System.StructuredQueryType.Boolean#True AND System.DeviceInterface.WinUsb.UsbVendorId:=6069 AND System.DeviceInterface.WinUsb.UsbProductId:=32";
      devices = await DeviceInformation.FindAllAsync(aqs);

      if (devices.Count > 0)
      {
        return await UsbDevice.FromIdAsync(devices[0].Id);
      }
      else
      {
        return null;
      }
    }


    public async Task<bool> Open()
    {
      this.device = await this.OpenDevice();

      if (this.device != null)
      {
        return true;
      }
      else
      {
        return false;
      }
    }


    //public static DaliResponse SendCommand(DaliCommand command, string devicePath)
    //{
    //  SafeFileHandle handle;
    //  DaliResponse res;
    //  byte[] cmd;

    //  lock (ioLock)
    //  {
    //    handle = Kernel32.CreateFile(devicePath, FileAccess.ReadWrite, FileShare.None, IntPtr.Zero, FileMode.Open, EFileAttributes.Overlapped, IntPtr.Zero);
    //    fs = new FileStream(handle, FileAccess.ReadWrite, 65, true);
    //    fs.ReadTimeout = 100;

    //    try
    //    {
    //      Trace.TraceInformation("Writing USB command");
    //      cmd = command.GetHidCommand();
    //      fs.Write(cmd, 0, cmd.Length);
    //      Trace.TraceInformation("Done writing USB command");

    //      Trace.TraceInformation("Reading USB command");

    //      if (command is QueryActualLevel)
    //      {
    //        res = new ActualLevel();
    //      }
    //      else if (command is QueryMinLevel)
    //      {
    //        res = new MinLevel();
    //      }
    //      else if (command is QueryMaxLevel)
    //      {
    //        res = new MaxLevel();
    //      }
    //      else if (command is QueryGroups_0_7)
    //      {
    //        res = new Groups_0_7();
    //      }
    //      else if (command is QueryGroups_8_15)
    //      {
    //        res = new Groups_8_15();
    //      }
    //      else
    //      {
    //        res = new DaliResponse();
    //      }
          
    //      fs.Read(res.HidCommand, 0, res.HidCommand.Length);
    //      Trace.TraceInformation("Done reading USB command");

    //      return res;
    //    }
    //    finally
    //    {
    //      fs.Close();
    //      fs.Dispose();
    //      handle.Close();
    //      handle.Dispose();
    //    }
    //  }
    //}
  }
}
